// Local
#include "testyeast.h"
#include "yeast.h"
#include "weight.h"
#include "volume.h"

// QtCore
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QString>

// QtXml
#include <QDomDocument>

// QtTest
#include <QTest>

// Entry point
QTEST_MAIN(testYeast)

void testYeast::Basics()
{
    QString Name("S04");
    int Version = 8974;
    YeastType Type = YTAle;
    YeastForm Form = YFDry;
    Volume Amount(0.2,Liter);
    Yeast zeYeast(Name,Version,Type,Form,Amount);

    QVERIFY(zeYeast._Name==Name);
    QVERIFY(zeYeast._Version==Version);
    QVERIFY(zeYeast._Type==Type);
    QVERIFY(zeYeast._Form==Form);
    QVERIFY(zeYeast._AmountV==Amount);
    QVERIFY(zeYeast._AmountIsWeight==false);

    Weight wAmout(11,Gram);
    Yeast zeYeast2(Name,Version,Type,Form,wAmout);

    QVERIFY(zeYeast2._Name==Name);
    QVERIFY(zeYeast2._Version==Version);
    QVERIFY(zeYeast2._Type==Type);
    QVERIFY(zeYeast2._Form==Form);
    QVERIFY(zeYeast2._AmountW==wAmout);
    QVERIFY(zeYeast2._AmountIsWeight==true);
    QVERIFY(zeYeast2!=zeYeast);
}

void testYeast::ParseXML()
{    
    Q_INIT_RESOURCE(test_yeast);

    // Parse XML
    QFile Input(":/burton.xml");
    QDomDocument doc("burton");
    QVERIFY(Input.open(QIODevice::ReadOnly));
    QVERIFY(doc.setContent(&Input));

    Yeast zeYeast;
    QVERIFY(0==zeYeast.CreateFromXMLNode(doc));
    QVERIFY(zeYeast._Name=="Burton Ale");
    QVERIFY(zeYeast._Version==1);
    QVERIFY(zeYeast._Type==YTAle);
    QVERIFY(zeYeast._Form==YFLiquid);
    QVERIFY(zeYeast._AmountIsWeight==false);
    QVERIFY(zeYeast._AmountV==Volume(0.0350130));
    QVERIFY(zeYeast._Laboratory=="White Labs");
    QVERIFY(zeYeast._ProductID=="WLP023");
    QVERIFY(zeYeast._MinTemperature==Temperature(20));
    QVERIFY(zeYeast._MaxTemperature==Temperature(22.7778));
    QVERIFY(zeYeast._Flocculation==YFlMedium);
    QVERIFY(zeYeast._Attenuation==72);
    QVERIFY(zeYeast._Notes=="Burton-on-trent yeast produces a complex character.  Flavors include apple, pear, and clover honey.");
    QVERIFY(zeYeast._BestFor=="All English styles including Pale Ale, IPA, Porter, Stout and Bitters.");
    QVERIFY(zeYeast._MaxReuse==5);
    QVERIFY(zeYeast._TimesCultured==0);
    QVERIFY(zeYeast._AddToSecondary==false);
}

void testYeast::ToXML()
{
    Yeast zeYeast;
    zeYeast._Name="Test Lager";
    zeYeast._Version=39876;
    zeYeast._Type=YTLager;
    zeYeast._Form=YFDry;
    zeYeast._AmountIsWeight=true;
    zeYeast._AmountW=Weight(0.22,Pound);
    zeYeast._Laboratory="Ze Brasseurs of ze Dimanche";
    zeYeast._ProductID="LG983";
    zeYeast._MinTemperature=Temperature(65,Farhenheit);
    zeYeast._MaxTemperature=Temperature(25,Celsius);
    zeYeast._Flocculation=YFlMedium;
    zeYeast._Attenuation=82.4;
    zeYeast._Notes="Test note for zeYeast";
    zeYeast._BestFor="Pichets, soulakins";
    zeYeast._TimesCultured=3098;
    zeYeast._MaxReuse=983702;
    zeYeast._AddToSecondary=true;

    QString zeString;
    QTextStream Stream(&zeString);
    QDomDocument docWrite("beerxml");
    zeYeast.ToXMLNode(docWrite);
    docWrite.save(Stream,2);
    qDebug() << "Dumping generated XML" << endl << zeString;

    QDomDocument docRead("beerxml");
    QVERIFY(docRead.setContent(zeString));
    Yeast parsedYeast;
    QVERIFY(0==parsedYeast.CreateFromXMLNode(docRead));
    QVERIFY(zeYeast==parsedYeast);
}
